<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row>
          <el-form ref="form" :model="form" label-width="80px" :inline="true">
            <el-col :span="18"
              ><el-form-item label="关键字">
                <el-input
                  v-model="form.keyword"
                  placeholder="根据编号搜索"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-button @click="quxiao">取消</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <el-alert type="info" show-icon>一共{{ counts }}条数据</el-alert>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="编号"></el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
            :formatter="formatter"
            width="80px"
          ></el-table-column>
          <el-table-column prop="questionIDs" label="题目编号"
            ><template slot-scope="scope">
              <el-link type="primary" v-for="(item, index) in scope.row.questionIDs" :key="index" @click="button(item)">
                {{ item.number }}</el-link
              >
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间"></el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)"
          ></el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率(%)"
          ></el-table-column>
          <el-table-column prop="userName" label="录入人"></el-table-column>
          <el-table-column label="操作"
            ><template slot-scope="scope">
              <el-button
                plain
                size="small"
                type="danger"
                icon="el-icon-delete"
                circle
                @click="buttonRemove(scope.row)"
              ></el-button
            ></template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :page-sizes="[10, 20, 50, 100]"
          :page-size="query.pagesize"
          :current-page="query.page"
          layout="total, prev, pager, next,sizes, jumper"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-card>
      <el-dialog
        title="题目预览"
        :visible.sync="dialogVisible"
        width="70%"
        >
  <tankuang :id='rows.id'>
  </tankuang>
  <span slot="footer" class="dialog-footer">

    <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
  </span>
</el-dialog>

    </div>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import EmployeeEnum from '@/constant/employees'
import tankuang from './tankuang.vue'

export default {

  data () {
    return {
      form: {
        keyword: ''
      },
      tableData: [
        {
          id: '',
          questionType: '',
          questionIDs: '',
          addTime: '',
          totalSeconds: '',
          accuracyRate: '',
          userName: ''
        }
      ],

      counts: 0,
      query: {
        page: 1, // 当前页码
        pagesize: 10 // 每页显示
      },
      dialogVisible: false,
      rows: {
      }
    }
  },
  components: { tankuang },
  created () {
    this.getlist()
  },
  methods: {
    // 获取列表
    async getlist () {
      const res = await randoms({ ...this.query, ...this.form })

      console.log(res)
      this.tableData = res.data.items
      this.counts = res.data.counts
      //   this.page = res.data.page
    },
    // 删除
    async buttonRemove (row) {
      // 提示框
      const hint = await this.$confirm(
        '此操作将永久删除该题组, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (hint === 'cancel') {
        this.$message('已取消！')
        return
      }
      // 执行删除
      try {
        await removeRandoms(row)
        await this.getlist()
        this.$message.success('删除成功！')
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败！' + err)
      }
    },
    button (rows) {
      this.dialogVisible = true

      this.$nextTick(() => {
        this.rows = rows
      })
    },
    // 分页
    // 每页条数发生改变触发
    handleSizeChange (newval) {
      this.query.pagesize = newval
      // console.log(this.query.pagesize)
      this.getlist()
    },
    // 页码发生改变触发
    handleCurrentChange (newval) {
      //   console.log(val)

      this.query.page = newval
      // console.log(this.query.page)
      this.getlist()
    },
    // 枚举
    formatter (row, column, cellValue, index) {
      const obj = EmployeeEnum.leaveType.find(
        (item) => item.questionType === cellValue
      )
      return obj ? obj.value : '未知'
    },

    onSubmit () {
      this.getlist()
    //   console.log(this.$refs.form)
    //   this.$refs.form.validate(valid => {
    //     console.log(valid)
    //   })
    },
    async quxiao () {
      this.form.keyword = ''
      await this.getlist()
    }
  }
}
</script>

<style scoped>
.el-input__inner {
  width: 30%;
}
.el-alert {
  margin-bottom: 20px;
}
</style>
